﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LeitorNFe;

namespace AtualizaCadastroNFe
{
    public partial class frmExport : Form
    {
        public frmExport()
        {
            InitializeComponent();
        }

        private void btnSalvar_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Text Files|*.txt";
            saveFileDialog1.Title = "Save an Text File";
            saveFileDialog1.ShowDialog();

            if (saveFileDialog1.FileName != "")
            {
                txtArquivo.Text = saveFileDialog1.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StreamWriter sw = null;

            try
            {
                if (txtArquivo.Text != "")
                {
                    LeitorNFe.FornecedorRepository fr = new LeitorNFe.FornecedorRepository();
                    sw = new StreamWriter(txtArquivo.Text);
                    string line;

                    foreach (FORNECEDOR fornec in fr.GetAll())
                    {
                        line = string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|{14}|{15}|{16}",
                                fornec.CNPJ.ToString().Trim(),
                                fornec.NOME == null ? "" : fornec.NOME.ToString().Trim(),
                                fornec.FANTASIA == null ? "" : fornec.FANTASIA.ToString().Trim(),
                                fornec.ENDERECO == null ? "" : fornec.ENDERECO.ToString().Trim(),
                                fornec.NUMERO == null ? "" : fornec.NUMERO.ToString().Trim(),
                                fornec.BAIRRO == null ? "" : fornec.BAIRRO.ToString().Trim(),
                                fornec.COD_MUN == null ? "" : fornec.COD_MUN.ToString().Trim(),
                                fornec.MUNICIPIO == null ? "" : fornec.MUNICIPIO.ToString().Trim(),
                                fornec.UF == null ? "" : fornec.UF.ToString().Trim(),
                                fornec.CEP == null ? "" : fornec.CEP.ToString().Trim(),
                                fornec.COD_PAIS == null ? "" : fornec.COD_PAIS.ToString().Trim(),
                                fornec.PAIS == null ? "" : fornec.PAIS.ToString().Trim(),
                                fornec.FONE == null ? "" : fornec.FONE.ToString().Trim(),
                                fornec.IE == null ? "" : fornec.IE.ToString().Trim(),
                                fornec.IM == null ? "" : fornec.IM.ToString().Trim(),
                                fornec.IEST == null ? "" : fornec.IEST.ToString().Trim(),
                                fornec.CNAE == null ? "" : fornec.CNAE.ToString().Trim());

                        sw.WriteLine(line);
                        sw.Flush();
                    }

                    MessageBox.Show("Exportado com sucesso!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                try
                {
                    sw.Close();
                    sw.Dispose();
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}
